﻿// -----------------------------------------------------------------------
// <copyright file="Cookies.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Model
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Text.RegularExpressions;


    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class Cookies
    {
        public static int LerCookie()
        {
            int resultado;
            if (HttpContext.Current.Request.Cookies["CarrinhoServicos"] != null)
                if (Regex.IsMatch(HttpContext.Current.Request.Cookies["CarrinhoServicos"].Value, "^[0-9]+$"))
                    if (Int32.TryParse(HttpContext.Current.Request.Cookies["CarrinhoServicos"].Value, out resultado))
                        return resultado;

            return 0;
        }

        public static bool ChecarId()
        {
            if (HttpContext.Current.Request.Cookies["CarrinhoServicos"] != null)
                return Regex.IsMatch(HttpContext.Current.Request.Cookies["CarrinhoServicos"].Value, "^[0-9]+$");
            return false;
        }

        public static void GravarCookie(string valor)
        {
            var cook = new HttpCookie("CarrinhoServicos");
            cook.HttpOnly = true;
            cook.Value = valor;
            cook.Expires = DateTime.Now.AddYears(1);
            HttpContext.Current.Response.Cookies.Add(cook);
        }

    }
}
